<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="nc.bs.framework.server.ISecurityTokenCallback" %>
<%@ page import="com.yonyou.chuan.log.LoggerUtil" %>
<%@ page import="com.yonyou.chuan.service.YhtPubService" %>
<%@ page import="com.yonyou.chuan.model.vo.YhtUserVO" %>

<%@ page import="cn.hutool.http.HttpUtil" %>
<%@ page import="cn.hutool.json.JSONUtil" %>
<%@ page import="com.yonyou.chuan.util.BipTokenUtil" %>
<%@ page import="com.yonyou.chuan.model.param.YonBipParam" %>

<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.util.Base64" %>
<%@ page import="nc.bs.framework.common.NCLocator" %>

<html>
<head>
	<title>yht登陆</title>
	<!-- @date          2024-09-26 -->
	<!-- @author        wangmob -->
	<!-- @discription   单点登陆,支持挤掉上一个登陆人,支持会话同人免登 -->
	<!-- @demo1			http://127.0.0.1:8088/nccloud/yht.jsp?target=uytrdfutrude&ticket=rfdghuytr -->
	<!-- Jsp内置对象System,request,response,session,out -->
</head>

<style>
	*{margin:0;padding:0}body{width:100%;height:100%;color:#6A6A6B;font-family:"Microsoft YaHei"}.unsupport-container{width:1200px;height:600px;position:absolute;left:50%;top:50%;margin-left:-400px;margin-top:-200px}.unsupport-msg{width:600px;margin-left:30px}.unsupport-container>div{float:left}.unsupport-image{width:344px;height:221px}.unsupport-image img{width:100%;height:100%}.unsupport-head-msg{font-size:30px}.unsupport-bottom-msg{font-size:18px;color:#252A36}.download-button{margin-top:95px}.download-button button{}.download-button a{display:inline-block;line-height:40px;text-decoration:none;color:#FFFFFF;font-size:14px;width:130px;height:40px;background:#E73D36;border:none;border-radius:3px;text-align:center}.prompt-msg{font-size:15px;color:#717171;margin-top:12px}
</style>

<body>
<div class="unsupport-container">
	<div class="unsupport-image">
		<img src="./html/image/browser_support.png">
	</div>
	<div class="unsupport-msg">
		<p class="unsupport-head-msg">登陆出错啦</p>
		<p class="unsupport-bottom-msg">
			<%
				try{
					long timestamp = System.currentTimeMillis();	// 时间戳
					String GLOBE_ORG = "GLOBLE00000000000000";      // 业务参数全局
					NCLocator.getInstance().lookup(ISecurityTokenCallback.class).token("NCSystem".getBytes(),"pfxx".getBytes());//模拟登录

					String Oringin = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
					LoggerUtil.printLogger("【待办登录-执行】: {}", timestamp);
					String target = request.getParameter("target");
					String loginId = request.getParameter("loginId");
					LoggerUtil.printLogger("【待办登录-票据】loginId: {}", loginId);
					LoggerUtil.printLogger("【待办登录-目标】target: {}", target);
					String realPath = (target != null && target.length() > 0) ? new String(Base64.getDecoder().decode(target)) : Oringin;
					LoggerUtil.printLogger("【待办登录-目标】realPath: {}", realPath);
					//TODO 业务参数注入
					String VerifyPath_Nw = "";    					//认证中心地址内网
					String qxg_secretKey = "";          			//认证秘钥
					if(loginId == null || loginId.isEmpty()){
						out.println("loginId-not-found: " + request.getQueryString());
						return;
					}
					// TODO 转换yht用户
					YhtUserVO yhtUserVO = NCLocator.getInstance().lookup(YhtPubService.class).queryByStaffId(loginId);
					if(yhtUserVO == null){
						out.println("yhtUserVO-not-found: " + loginId);
						return;
					}
					// TODO 登录友互通
					String bip_origin = "bip_origin";		//bip数据中心地址
					String bip_ucctid = "bip_ucid";  		//bip集成认证中心
					String bip_appKey = "bip_appKey";    	//bip应用参数
					String bip_secret = "bip_secret";    	//bip应用参数
					String bip_Token = BipTokenUtil.getAccessToken(bip_origin + YonBipParam.OPEN_API_URL, bip_appKey, bip_secret);
					String yhtPath = String.format(bip_origin + YonBipParam.THIRD_LOGIN_URL + "?access_token=%s",bip_Token);
					String jsonBody = String.format("{\"userId\":\"%s\",\"thirdUcId\":\"%s\"}",yhtUserVO.getYhtId(),bip_ucctid);
					LoggerUtil.printLogger("【待办登录-友互通】,请求地址: {}",yhtPath);
					LoggerUtil.printLogger("【待办登录-友互通】,请求参数: {}",jsonBody);
					String yhtResponse = HttpUtil.createPost(yhtPath).body(jsonBody).execute().body();
					LoggerUtil.printLogger("【待办登录-友互通】,响应报文: {}",yhtResponse);
					String yht_auth = JSONUtil.parseObj(yhtResponse).getJSONObject("data").getStr("code");
					String yht_origin = JSONUtil.parseObj(yhtResponse).getJSONObject("data").getStr("curYhtEnvironment");
					// TODO 封装YonBIP单点登录URLs
					String redirect = URLEncoder.encode(bip_origin + "/login?service=" + URLEncoder.encode(realPath,"UTF-8"),"UTF-8");
					String loginPath = yht_origin + "/cas/thirdOauth2CodeLogin?thirdUCId=%s&code=%s&service=%s";
					String finalPath = String.format(loginPath, bip_ucctid, yht_auth, redirect);
					LoggerUtil.printLogger("【待办登录-友互通】,执行跳转: {}", finalPath);
					response.sendRedirect(finalPath);
				} catch (Exception e) {
					out.println("【待办登录-发生异常】," + e.getMessage());
				}
			%>
		</p>
	</div>
</div>
</body>
</html>